from PIL import Image


def crop(fn, x, y):
  im = Image.open(fn)
  w, h = im.size
  result = []
  sw = int(w / x)
  sh = int(h / y)
  for j in range(y):
    result.append([])
    for i in range(x):
      left  = i * sw
      up    = j * sh
      right = (i + 1) * sw
      down  = (j + 1) * sh
      newIm = im.crop((left, up, right, down))
      # newIm.show()
      result[j].append(newIm)
  return result


result = crop("./house.jpg", 4, 2)
for j in range(2):
  for i in range(4):
    result[j][i].save("./house-%s-%s.jpg" % (i, j), quality=95)
